import btree4j.*;

import java.io.File;
import java.util.Random;

public class BTreeTest {

    public static void main(String[] args) {
        // 测试文件路径
        File btreeFile = new File("btree_test.bt");
        File btreeIndexFile = new File("btreeindex_test.bt");
        File btreeIndexDupFile = new File("btreeindexdup_test.bt");

        // 清理旧文件
        btreeFile.delete();
        btreeIndexFile.delete();
        btreeIndexDupFile.delete();

        // 测试数据
        Value[] keys = new Value[10];
        long[] values = new long[10];
        Random random = new Random();

        for (int i = 0; i < 10; i++) {
            keys[i] = new Value(("key" + i).getBytes());
            values[i] = random.nextLong();
        }

        // 测试 BTree
        testBTree(btreeFile, keys, values);
        // 测试 BTreeIndex
        testBTreeIndex(btreeIndexFile, keys, values);
        // 测试 BTreeIndexDup
        testBTreeIndexDup(btreeIndexDupFile, keys, values);
    }

    private static void testBTree(File file, Value[] keys, long[] values) {
        System.out.println("\n===== BTree Test =====");
        BTree btree = new BTree(file, true);

        try {
            btree.init(true);
        } catch (BTreeException e) {
            e.printStackTrace();
            return;
        }

        // 插入数据
        for (int i = 0; i < keys.length; i++) {
            try {
                long ptr = btree.addValue(keys[i], values[i]);
                System.out.println("Inserted into BTree: key=" + keys[i] + ", value=" + values[i] + ", ptr=" + ptr);
            } catch (BTreeException e) {
                e.printStackTrace();
            }
        }

        // 查找数据
        for (int i = 0; i < keys.length; i++) {
            try {
                long ptr = btree.findValue(keys[i]);
                System.out.println("Found in BTree: key=" + keys[i] + ", ptr=" + ptr);
            } catch (BTreeException e) {
                e.printStackTrace();
            }
        }

        // 删除数据
        for (int i = 0; i < keys.length; i++) {
            try {
                long ptr = btree.removeValue(keys[i]);
                System.out.println("Removed from BTree: key=" + keys[i] + ", ptr=" + ptr);
            } catch (BTreeException e) {
                e.printStackTrace();
            }
        }

        try {
            btree.close();
        } catch (BTreeException e) {
            e.printStackTrace();
        }
    }

    private static void testBTreeIndex(File file, Value[] keys, long[] values) {
        System.out.println("\n===== BTreeIndex Test =====");
        BTreeIndex btreeIndex = new BTreeIndex(file);

        try {
            btreeIndex.init(true);
        } catch (BTreeException e) {
            e.printStackTrace();
            return;
        }

        // 插入数据
        for (int i = 0; i < keys.length; i++) {
            try {
                long ptr = btreeIndex.addValue(keys[i], values[i]);
                System.out.println("Inserted into BTreeIndex: key=" + keys[i] + ", value=" + values[i] + ", ptr=" + ptr);
            } catch (BTreeException e) {
                e.printStackTrace();
            }
        }

        // 查找数据
        for (int i = 0; i < keys.length; i++) {
            try {
                long ptr = btreeIndex.findValue(keys[i]);
                System.out.println("Found in BTreeIndex: key=" + keys[i] + ", ptr=" + ptr);
            } catch (BTreeException e) {
                e.printStackTrace();
            }
        }

        // 删除数据
        for (int i = 0; i < keys.length; i++) {
            try {
                long ptr = btreeIndex.removeValue(keys[i]);
                System.out.println("Removed from BTreeIndex: key=" + keys[i] + ", ptr=" + ptr);
            } catch (BTreeException e) {
                e.printStackTrace();
            }
        }

        try {
            btreeIndex.close();
        } catch (BTreeException e) {
            e.printStackTrace();
        }
    }

    private static void testBTreeIndexDup(File file, Value[] keys, long[] values) {
        System.out.println("\n===== BTreeIndexDup Test =====");
        BTreeIndexDup btreeIndexDup = new BTreeIndexDup(file);

        try {
            btreeIndexDup.init(true);
        } catch (BTreeException e) {
            e.printStackTrace();
            return;
        }

        // 插入数据
        for (int i = 0; i < keys.length; i++) {
            try {
                long ptr = btreeIndexDup.addValue(keys[i], values[i]);
                System.out.println("Inserted into BTreeIndexDup: key=" + keys[i] + ", value=" + values[i] + ", ptr=" + ptr);
            } catch (BTreeException e) {
                e.printStackTrace();
            }
        }

        // 查找数据
        for (int i = 0; i < keys.length; i++) {
            try {
                long ptr = btreeIndexDup.findValue(keys[i]);
                System.out.println("Found in BTreeIndexDup: key=" + keys[i] + ", ptr=" + ptr);
            } catch (BTreeException e) {
                e.printStackTrace();
            }
        }

        // 删除数据
        for (int i = 0; i < keys.length; i++) {
            try {
                long ptr = btreeIndexDup.removeValue(keys[i]);
                System.out.println("Removed from BTreeIndexDup: key=" + keys[i] + ", ptr=" + ptr);
            } catch (BTreeException e) {
                e.printStackTrace();
            }
        }

        try {
            btreeIndexDup.close();
        } catch (BTreeException e) {
            e.printStackTrace();
        }
    }
}